package com.hexacta.hrs.sandbox.tasks2.data;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hexacta.hrs.sandbox.tasks2.model.Category;

@SuppressWarnings("unchecked")
public class CategoryDaoImpl extends HibernateDaoSupport implements CategoryDao {

	public List<Category> findAll() {
		return getHibernateTemplate().find(
				"from " + Category.class.getSimpleName());
	}

	public Category findById(Long id) {
		return (Category) getHibernateTemplate().get(Category.class, id);
	}

	public void addCategory(Category newCategory) {
		getHibernateTemplate().save(newCategory);

	}

	public Category findByName(String selectedCategory) {

		List<Category> queryRes;

		queryRes = getHibernateTemplate().find(
				"from " + Category.class.getSimpleName() + " where name='"
						+ selectedCategory + "'");

		if (queryRes.iterator().hasNext()) {
			return queryRes.iterator().next();
		}

		return null;
	}
}
